package cn.tk.utils;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by denglw on 2017-12-08.<br/>
 * Desc: 是否为空工具类
 */
public final class EmptyUtil {

    private EmptyUtil() { }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static boolean nonNull(Object object) {
        return object != null;
    }

    public static boolean anyNull(Object... objects) {
        for (Object object : objects) {
            if (object == null) {
                return true;
            }
        }
        return false;
    }

    public static boolean allNull(Object... objects) {
        for (Object object : objects) {
            if (object != null) {
                return false;
            }
        }
        return true;
    }

    public static boolean blank(CharSequence cs) {
        return cs == null || cs.toString().trim().length() == 0;
    }

    public static boolean nonBlank(CharSequence cs) {
        return !blank(cs);
    }

    public static boolean anyBlank(CharSequence... css) {
        for (CharSequence cs : css) {
            if (blank(cs)) {
                return true;
            }
        }
        return false;
    }

    public static boolean allBlank(CharSequence... css) {
        for (CharSequence cs : css) {
            if (!blank(cs)) {
                return false;
            }
        }
        return true;
    }

    public static String nullToEmpty(String string) {
        return string == null ? "" : string;
    }

    public static boolean emptyColl(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    public static boolean emptyMap(Map<?, ?> map) {
        return map == null || map.size() == 0;
    }

    public static boolean nullOrZero(Number number) {
        if (isNull(number)) {
            return true;
        }
        double doubleValue = number.doubleValue();
        BigDecimal decimal = new BigDecimal(String.valueOf(doubleValue));
        return decimal.compareTo(new BigDecimal(0)) == 0;
    }

    public static long nullToZero(Number number) {
        return isNull(number) ? 0L : number.longValue();
    }

    public static <T> List<T> nullToEmptyList(List<T> list) {
        return list == null ? Collections.emptyList() : list;
    }

    public static <T> Set<T> nullToEmptySet(Set<T> set) {
        return set == null ? Collections.emptySet() : set;
    }
}
